/* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved.
miniob is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details. */

//
// Created by Auzdora on 2023/09/07.
//

#pragma once

#include "sql/expr/tuple.h"
#include "sql/expr/tuple_cell.h"
#include "sql/operator/operator.h"
#include "rc.h"
#include "sql/parser/parse_defs.h"
#include <vector>

class AggregationOperator : public Operator
{
public:
  AggregationOperator(const std::vector<AggrInfo> &aggr_infos) : aggr_infos_(aggr_infos)
  {
    aggr_results_.reserve(aggr_infos.size());
  }

  virtual ~AggregationOperator() = default;

  void add_projection(const Table *table, const FieldMeta *field);

  RC open() override;
  RC next() override;
  RC close() override;
  void do_aggr(int idx, bool init,  const TupleCell &cell);
  RC do_min(int idx, const TupleCell &cell);
  RC do_max(int idx, const TupleCell &cell);
  RC do_avg(int idx, const TupleCell &cell);
  RC do_count(int idx);
  const TupleCell &get_tuple_cell_as(int idx);
  const AggrType get_type_at(int idx);

//   int tuple_cell_num() const
//   {
//     return tuple_.cell_num();
//   }

  RC tuple_cell_spec_at(int index, const TupleCellSpec *&spec) const;

  Tuple * current_tuple() override;
private:
    std::vector<AggrInfo> aggr_infos_;
    std::vector<TupleCell> aggr_results_;
};
